//
//  CellectDatabase.m
//  FMSQL
//
//  Created by dllo on 15/5/28.
//  Copyright (c) 2015年 李凯萌. All rights reserved.
//

#import "CellectDatabase.h"
#import "CellectDB.h"
#import "CellectModel.h"
@implementation CellectDatabase

+ (void)commitSql:(NSString *)str
{
    //获得数据库指针
    sqlite3 *db = [CellectDB openDB];
    //执行sql语句
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil); //判断是否执行成功
    if (result == SQLITE_OK) {
//                NSLog(@"操作成功");
    } else {
        //        NSLog(@"操作失败");
    }
}
+ (void)insertWithModel:(CellectModel *)model
{
    // sql语句
    NSString *str = [NSString stringWithFormat:@"insert into FMCellect(titleName,imageName,videoUrl,people) values('%@','%@','%@','%@')",model.titleName, model.imageName, model.videoUrl, model.people];
    [CellectDatabase commitSql:str];
    
}
+ (void)deleteWithBookId:(CellectModel *)model
{
    NSString *str = [NSString stringWithFormat:@"delete  from FMCellect where titleName = '%@'",model.titleName];
    [CellectDatabase commitSql:str];
}
+ (NSArray *)selectAll
{
    NSString *str = @"select titleName,imageName,videoUrl,people from FMCellect";
    
    sqlite3 *db = [CellectDB openDB];
    //定义执行结果接收者
    sqlite3_stmt *stmt =nil;
    //查询结果所在的位置(结果就再stmt中)
    
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    NSMutableArray *array = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *titleName = sqlite3_column_text(stmt, 0);
            const unsigned char *imageName = sqlite3_column_text(stmt, 1);
            const unsigned char *videoUrl = sqlite3_column_text(stmt, 2);
            const unsigned char *people = sqlite3_column_text(stmt, 3);
            NSString *title = [NSString stringWithUTF8String:(const char *)titleName];
            NSString *image = [NSString stringWithUTF8String:(const char *)imageName];
            NSString *video = [NSString stringWithUTF8String:(const char *)videoUrl];
            NSString *peo = [NSString stringWithUTF8String:(const char *)people];
            CellectModel *model = [[CellectModel alloc] initWithTitleName:title imageName:image videoUrl:video people:peo];
            [array addObject:model];
        }
    }
    return array;
    
}

+ (CellectModel *)selectWithTitle:(NSString *)title
{
    NSString *str = [NSString stringWithFormat:@"select titleName,imageName,videoUrl,people from FMCellect where titleName='%@'", title];
    
    
    sqlite3 *db = [CellectDB openDB];
    //定义执行结果接收者
    sqlite3_stmt *stmt =nil;
    //查询结果所在的位置(结果就再stmt中)
    
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *titleName = sqlite3_column_text(stmt, 0);
            const unsigned char *imageName = sqlite3_column_text(stmt, 1);
            const unsigned char *videoUrl = sqlite3_column_text(stmt, 2);
            const unsigned char *people = sqlite3_column_text(stmt, 3);
            NSString *title = [NSString stringWithUTF8String:(const char *)titleName];
            NSString *image = [NSString stringWithUTF8String:(const char *)imageName];
            NSString *video = [NSString stringWithUTF8String:(const char *)videoUrl];
            NSString *peo = [NSString stringWithUTF8String:(const char *)people];
            CellectModel *model = [[CellectModel alloc] initWithTitleName:title imageName:image videoUrl:video people:peo];
            return model;
        }
    }
    return nil;
    
}

@end
